package com;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/*
问题：
绘棋之后五子连线没有展示出来
 */
public class PlayGame  extends JFrame implements ActionListener, MouseListener {
    //添加数据处理的成员
    Datahandling datahandling=new Datahandling();

    //棋盘背景图片
    BufferedImage bgimage=null;
    //棋子图片
    BufferedImage blackchess=null;
    BufferedImage whitechess=null;
    //背景图片
    BufferedImage play=null;
    //头像
    BufferedImage player=null;
    //需要使用的按钮
    JButton New=new JButton("新 局");
    JButton away=new JButton("退 出");
    JButton returnback=new JButton("返 回");

    //map[x][y]=1对应黑子，map[x][y]=0对应白子
    private static final int BLACK = 1;
    private static final int WHITE = 2;
    private static final int EMPTY = 0;

    //记录黑白子的个数，用于判定是否平局
    int darkstep,whitestep;

    //记录鼠标点击的位置
    int x,y;
    //用数组记录黑白子的位置
    int map[][]=new int[15][15];

    int flag=1;//flag为1黑子，flag为2白子

    JPanel jPanel=new chessboard();//棋盘

    boolean iswin=datahandling.isWin(map);
    //--------------------------------------------------------------

    /*
     *负责人:微风
     *功能:清空棋盘，将map数组值都赋0，darkstep和whitestep也清为0，flag值默认为1
     *参数:void
     *返回值：void
     */
    public void initdata(){
        for(int i=0;i<map.length;i++){
            for(int j=0;j<map[i].length;j++){
                map[i][j]=0;
            }
        }
        iswin=false;
        darkstep=0;
        whitestep=0;
    }

    /*
     *负责人:微风
     *功能:调用initjButton();initjFrame(),initdata()
     */
    public PlayGame() {
        initjButton();
        initJFrame();
        initdata();
        this.setVisible(true);
    }

    /*
      *负责人:凯子
      *功能: 设定按钮的大小和位置，颜色
      *      并为按钮绑定ActionListener,
             注：用本界面类实现ActionListener接口，在本类中重写actionPerformed方法
             当按钮被点击时能够展示对应界面
      *参数：void
      *返回值：void
      */
    private void initjButton() {
        //设置字体
        Font font = new Font("微软雅黑",1,24);
        //设置颜色
        Color color=new Color(205,133,33);
        New.setFont(font);
        //颜色，大小位置
        New.setBounds(500,750,128,47);
        New.setBackground(color);
        New.setFocusPainted(false);
        New.addActionListener(this);

        away.setFont(font);
        away.setBounds(300,750,128,47);
        away.setBackground(color);
        away.setFocusPainted(false);
        away.addActionListener(this);

        returnback.setFont(font);
        returnback.setBounds(700,750,128,47);
        returnback.setBackground(color);
        returnback.setFocusPainted(false);
        returnback.addActionListener(this);

        this.getContentPane().add(New);
        this.getContentPane().add(away);
        this.getContentPane().add(returnback);
    }
    /*

     *负责人:微风
     *功能:创建游戏界面，设置界面的大小：968*900
     *     标题字体格式，
     *     使界面居中显示
     *     用image文件夹下的background.jpg初始化bgimag对象
           并将bgimage添加进界面
     *参数:void
     *返回值：void
     */
    private void initJFrame() {
        this.setTitle("游戏界面");
        this.setFont(new Font("黑体",Font.BOLD,50));
        this.setSize(968,900);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);//关闭窗口并且不影响其他界面
        this.setAlwaysOnTop(true);

        try {
            player=ImageIO.read(new File("Image/player.jpg"));
            play=ImageIO.read(new File("Image/play.jpg"));
            blackchess=ImageIO.read(new File("Image/blackChess.png"));
            whitechess=ImageIO.read(new File("Image/whiteChess.png"));
            bgimage= ImageIO.read(new File("Image/background.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.addMouseListener(this);
        this.setLayout(null);
        jPanel.setBounds(0,0,this.getWidth(),this.getHeight());
        this.getContentPane().add(jPanel);

    }

    /*
     难 点1：根据map[i][j]换算出x与i,y与j的关系，然后完成画棋
     *负责人:竹音
     *功能:给游戏界面添加背景颜色
     *     展示游戏信息
     *     添加背景图片bgimage
     *     绘制棋盘
     *     根据map数组绘制棋子
     *     
     *
     *参数:void
     *返回值：void
     */

    class chessboard extends JPanel{
        public  void paint(Graphics g){
            super.paint(g);
//            g.setColor(new Color(255, 218, 185));
//            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.drawImage(play,0,0,this);
            g.drawImage(bgimage, 230, 33, this);
            g.drawImage(player,10,400,200,300,this);
            g.setColor(Color.BLACK);
            Font font=new Font("微软雅黑",Font.BOLD,40);
            Font font1=new Font("微软雅黑",Font.BOLD,25);
            g.setFont(font);
            g.drawString("游戏信息",20,50);
            g.setFont(font1);
            String turn=flag==1?"黑方":"白方";
            g.drawString("轮到"+turn,30,190);
            g.drawString("白子数："+whitestep,30,260);
            g.drawString("黑子数: "+darkstep,30,330);
            //绘制棋盘
            g.setFont(new Font("黑体", Font.BOLD, 50));
            g.setColor(Color.BLACK);
            for (int mx = 0; mx < 15; ++mx) {
                g.drawLine(261, mx * 47 + 63, 919, mx * 47 + 63);
                g.drawLine(mx * 47 + 261, 64, mx * 47 + 261, 721);
            }
            // 放置棋子
            for (int i = 0; i < 15; i++) {
                for (int j = 0; j < 15; j++) {
                    if (map[i][j] == BLACK) {
                        int tempx = i * 47 + 250;
                        int tempy = j * 47 + 50;

                        g.drawImage(blackchess,tempx,tempy,30,30,this);
                    }
                    if (map[i][j] == WHITE) {

                        int tempx = i * 47 + 255;
                        int tempy = j * 47 + 50;
                        g.drawImage(whitechess,tempx,tempy,30,30,this);
                    }
                }
            }

        }
    }

    /*
     *负责人:伏愿
     *功能:重写ActionListener方法,
     *     若按钮退出被点击，则直接退出游戏
     *     若按返回按钮销毁当前界面，再创建一个MenuView
     *     若按钮新局被点击，则直接执行initdata()语句，并调用repaint()方法
     *
     *参数：ActionEvent
     *返回值:void
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        Object o=e.getSource();
        if(o==away){
            System.exit(0);
        }
        if(o==New){
            initdata();
            repaint();
        }
        if(o==returnback){
            this.dispose();
            new MenuView();
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    /*难点2：
     *负责人:h
     *     换算x,y和i,j的关系进行赋值
     *功能:根据鼠标点击获取x,y
     *     换算出对应的map中x对应的i,y对应的j
     *     然后判断map[i][j]是否为空，若为空
     *     再根据当前的落子标记，为map[i][j]赋值，完成落子，调用repaint()
     *     若map[i][j]不为空，弹出对应提示
     *     落子成功后现利用darkstep和whitestep的值判断是否平局，
     *     然后调用datahandling的isWin方法
     *参数:void
     *返回值：void
     */
    @Override
    public void mousePressed(MouseEvent e) {
        x=e.getX();
        y=e.getY();
        //计算x,y对应棋盘中的x,y下标
        //平局或已经分出胜负时不再进行落子

        if(!iswin){
            if(x>=247&&x<=951&&y>=77&&y<=781){
                double tx1=((double) x-270.0D)/47.0D;
                double ty1=((double)y-100.0D)/47.0D;
                int tx2=(x-270)/47;
                int ty2=(y-100)/47;
                if(tx1-(double)tx2<0.5D&&tx1-(double)tx2>-0.5D){
                    x=tx2;
                }
                else{
                    x=tx2+1;
                }
                if(ty1-(double)ty2<0.5D&&ty1-(double)ty2>-0.5D){
                    y=ty2;
                }
                else{
                    y=ty2+1;
                }
                //System.out.println("x:"+x+" "+"y:"+y);
                //只有当点击位置能落子时，才更新棋盘数组
                if(map[x][y]==0){//当map[x][y]没有棋子时
                    if(flag==1){
                        map[x][y]=BLACK;
                        darkstep++;
                        flag=2;
                    }
                    else if(flag==2){
                        map[x][y]=WHITE;
                        flag=1;
                        whitestep++;
                    }
                    this.repaint();
                    //判断输赢
                    iswin=datahandling.isWin(map);
                    //判断是否平局
                    if(whitestep+darkstep==map.length*map[0].length){
                        JOptionPane.showMessageDialog(this,"平局了哟！请点击新局再来一次吧");

                    }
                    if(iswin){
                        String winner=flag==1?"白方":"黑方";
                        JOptionPane.showMessageDialog(this,"欧耶！"+winner+"获胜~~");
                    }
                }
                else{
                    JOptionPane.showMessageDialog(this,"当前位置已经有棋子，请重新落子！");
                }
                //判断是否获胜---更新iswin的值

            }
        }


    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
